﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WyUnity.Utility.IOC
{
	/// <summary>
	/// 对象容器管理器
	/// </summary>
	public static class ObjectContainerManager
	{
		private static IObjectContainer _instanceObjectContainer;
		private static object _lockObject = new object();

		/// <summary>
		/// 获取系统当前对象容器
		/// </summary>
		public static IObjectContainer GetCurrentObjectContainer()
		{
			if (_instanceObjectContainer == null)
			{
				lock (_lockObject)
				{
					if (_instanceObjectContainer == null)
					{
						_instanceObjectContainer = CreateObjectContainer();
					}
				}
			}
			return _instanceObjectContainer;
		}

		private static IObjectContainer CreateObjectContainer()
		{
			try
			{
				var iocTypeName = Util.GetAppSetting("ioc");
				if (string.IsNullOrEmpty(iocTypeName)) throw new Exception("必须在config文件中设置ioc容器名称");

				var ioctype = Type.GetType(iocTypeName);

				IObjectContainer rlt = Activator.CreateInstance(ioctype) as IObjectContainer;
				if (rlt == null) throw new Exception("无法实例化IOC对象容器:" + Util.GetAppSetting("ioc"));
				return rlt;
			}
			catch (Exception ex)
			{
				throw new Exception("实例化IOC对象容器错误", ex);
			}
		}

		public static IObjectContainer GetObjectContainer(string name)
		{
			throw new NotImplementedException();
		}
	}
}
